// 设置图片上传预览插件
export default function selectImageUpload(el) {
  el.onchange = function () {
    if (!el.value) {
      el.parentNode.style.backgroundImage = ''
    } else {
      uploadImg()
    }
  }
  uploadImg()
  function uploadImg() {
    if (el.value && !/.jpg|.jepg|.gif|.png|.bmp/i.test(el.value)) {
      $toast('图片格式无效，请重新选择')
      return false
    }
    if (el.files && el.files[0]) {
      el.parentNode.style.backgroundImage = 'url(' + window.URL.createObjectURL(el.files[0]) + ')'
    }
    else {
      //IE下，使用滤镜
      el.select()
      window.parent.document.body.focus()
      var imgSrc = document.selection.createRange().text
      el.parentNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
      el.parentNode.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc
      document.selection.empty()
    }
    return true
  }
}